package com.tiger.cloud.service.crm;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.tiger.cloud.common.mybatis.MybatisTigerDao;
import com.tiger.cloud.common.mybatis.TigerBaseService;
import com.tiger.cloud.common.sms.request.SmsSendRequest;
import com.tiger.cloud.common.sms.response.SmsSendResponse;
import com.tiger.cloud.common.sms.util.ChuangLanSmsUtil;
import com.tiger.cloud.dao.crm.AppSmsDao;
import com.tiger.cloud.enums.AppSmsSendStatus;
import com.tiger.cloud.enums.AppSmsType;
import com.tiger.cloud.model.crm.AppSms;

/**
 * 短信
 */
@Service("appSmsService")
public class AppSmsService extends TigerBaseService<AppSms> implements IAppSmsService {

	@Autowired
	private AppSmsDao appSmsDao;

	@Override
	public MybatisTigerDao<AppSms> getEntityDao() {
		return appSmsDao;
	}

	/**
	 * 这里发送的是没有token的这类用户短信
	 * 
	 * @param msg
	 * @param userId
	 * @param appSmsType
	 * @return
	 */
	@Override
	public AppSms sendToUserID(String msg, Long userId,String phone, AppSmsType appSmsType,Date activeTime) {
		AppSms appVerifCode = new AppSms();
		appVerifCode.setMessage(msg);
		AppSmsSendStatus sendStatus=AppSmsSendStatus.FAIL;
		try {
			SmsSendResponse sendResponse=sendSms(msg, phone);
			if(StringUtils.isEmpty(sendResponse.getCode())&&sendResponse.getCode().equals("0")){
				sendStatus=AppSmsSendStatus.SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		appVerifCode.setActiveTime(activeTime);
		appVerifCode.setUserId(userId);
		appVerifCode.setSendStatus(sendStatus);
		appVerifCode.setPhone(phone);
		appSmsDao.insert(appVerifCode);
		return appVerifCode;
	}

	public static final String charset = "utf-8";
	public static String account = "N1421116";
	public static String pswd = "YjXcvnfx911ed4";
	public static String smsSingleRequestServerUrl = "http://smssh1.253.com/msg/send/json";

	private SmsSendResponse sendSms(String content,String phone) {
		// 短信内容
		String report= "true";
		SmsSendRequest smsSingleRequest = new SmsSendRequest(account, pswd, content, phone,report);
		String requestJson = JSON.toJSONString(smsSingleRequest);
		System.out.println("before request string is: " + requestJson);
		String response = ChuangLanSmsUtil.sendSmsByPost(smsSingleRequestServerUrl, requestJson);
		System.out.println("response after request result is :" + response);
		SmsSendResponse smsSingleResponse = JSON.parseObject(response, SmsSendResponse.class);
		System.out.println("response  toString is :" + smsSingleResponse);
		return smsSingleResponse;
	}

	/**
	 * 这里发送的是没有token的这类用户短信
	 * 
	 * @param msg
	 * @param userId
	 * @param appSmsType
	 * @return
	 */
	@Override
	public AppSms sendToUID(String msg,String phone, AppSmsType appSmsType,Date activeTime) {
		AppSms appVerifCode = new AppSms();
		appVerifCode.setMessage(msg);
		AppSmsSendStatus sendStatus=AppSmsSendStatus.FAIL;
		try {
			SmsSendResponse sendResponse=sendSms(msg, phone);
			if(StringUtils.isEmpty(sendResponse.getCode())&&sendResponse.getCode().equals("0")){
				sendStatus=AppSmsSendStatus.SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		appVerifCode.setActiveTime(activeTime);
		appVerifCode.setSendStatus(sendStatus);
		appVerifCode.setPhone(phone);
		appSmsDao.insert(appVerifCode);
		return appVerifCode;
	}

	@Override
	public AppSms sendToUserID(String msg, Long userId, String phone, AppSmsType appSmsType) {
		return sendToUserID(msg, userId, phone, appSmsType,null);
	}

}